package com.kc.stand.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kc.saf.domain.CoreManagement;
import com.kc.stand.domain.*;
import com.kc.stand.service.FileManageService;
import com.kc.stand.service.FileRecordService;
import com.kc.stand.service.ModifyDetailService;
import com.kc.stand.service.StandFineListService;
import com.kc.stand.vo.ArticleVo;
import com.kc.sys.common.R;
import com.kc.sys.domain.Station;
import com.kc.sys.domain.User;
import com.kc.sys.service.StationService;
import com.kc.sys.util.ChineseNumToArabicNumUtil;
import com.kc.sys.util.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 * 各站站细详情
 * @author 老杜
 * @since 2019-12-30
 */
@RestController
@RequestMapping("/fileManage")
public class FileManageController {
    @Autowired
    private FileManageService fileManageService;
    @Autowired
    private StandFineListService standFineListService;
    @Autowired
    private FileRecordService fileRecordService;
    @Autowired
    private StationService stationService;
    @Autowired
    private ModifyDetailService modifyDetailService;
    //根据Id查询详情
    @RequestMapping("findStandFileTree")
    public R findArticleTree(HttpServletRequest request) {
        String nodeId = request.getParameter("nodeId");
        System.out.println("nodeId"+nodeId);
        FileManage fileManage = fileManageService.getById(nodeId);
        System.out.println(fileManage.getTreeContext());
        return R.ok().data("item",fileManage);
    }

    //根据ID更新站细详情
    @RequestMapping("updateStandFileById/{nodeid}")
    public R updateArticleById(@PathVariable("nodeid") Integer id,@RequestParam("stationId") Integer stationId1, FileManage fileManage){
        User user = (User) WebUtils.getSession().getAttribute("user");
        Map<String, Integer> map = loadCoreMaxOrderNum();
        Integer num = map.get("value");
        try {
            if (null!=fileManage.getSid()){
                List<Integer> sid = fileManage.getSid();
                if (sid.size()>0){
                    for (Integer stationId : sid) {
                        QueryWrapper<StandFileList> queryWrapper = new QueryWrapper<>();
                        queryWrapper.eq("sid",stationId);
                        StandFileList standFileList = standFineListService.getOne(queryWrapper);
                        Integer fileListId = standFileList.getId();
                        QueryWrapper<FileManage> fileManageQueryWrapper = new QueryWrapper<>();
                        fileManageQueryWrapper.eq("stand_fileId",fileListId);
                        fileManageQueryWrapper.eq("tree_id",fileManage.getNodeId());
                        fileManageQueryWrapper.eq("tree_pid",fileManage.getParentId());
                        FileManage fileManage1 = fileManageService.getOne(fileManageQueryWrapper);
                        fileManage1.setTreeContext(fileManage.getTreeContext());

                       //添加修改记录
                       /* FileRecord fileRecord = new FileRecord();
                        Station station = stationService.getById(stationId);
                        Date date = new Date();
                        SimpleDateFormat sdf1 =new SimpleDateFormat("yyyy" );
                        String str1 = sdf1.format(date);
                        String s = ChineseNumToArabicNumUtil.arabicNumToChineseNum(standFileList.getFrequency() + 1);
                        fileRecord.setArticleTitle(station.getTitle()+"《站细》"+str1+"第"+s+"次修改");
                        fileRecord.setReviseDate(new Date());
                        fileRecord.setPutPerson(user.getId());
                        fileRecord.setPutStation(user.getDeptid());
                        fileRecord.setExaminePerson(user.getId());
                        fileRecord.setArticleEntry(fileManage1.getId());
                        fileRecordService.save(fileRecord);*/
                        //添加修改条目
                        ModifyDetail modifyDetail = new ModifyDetail();
                        modifyDetail.setRecordId(num);
                        modifyDetail.setModifyPid(fileManage1.getId()-1);
                        modifyDetail.setModifyTreeId(fileManage1.getId());
                        FileManage fileManage2 = fileManageService.getById(fileManage1.getId() - 1);
                        FileManage fileManage3 = fileManageService.getById(fileManage1.getId());
                        modifyDetail.setModifyTreeTitle(fileManage2.getTreeTitle());
                        modifyDetail.setModifyTreeContext(fileManage3.getTreeContext());
                        modifyDetail.setModifyDate(new Date());
                        modifyDetail.setModifySid(stationId);
                        modifyDetailService.save(modifyDetail);
                        fileManageService.updateById(fileManage1);
                    }
                }
            }
            //添加修改记录
            /*FileRecord fileRecord = new FileRecord();
            Station station = stationService.getById(stationId1);
            Date date = new Date();
            SimpleDateFormat sdf1 =new SimpleDateFormat("yyyy" );
            String str1 = sdf1.format(date);
            QueryWrapper<StandFileList> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("sid",stationId1);
            StandFileList standFileList = standFineListService.getOne(queryWrapper);
            standFileList.setFrequency(standFileList.getFrequency()+1);
            standFineListService.updateById(standFileList);
            String s = ChineseNumToArabicNumUtil.arabicNumToChineseNum(standFileList.getFrequency() + 1);
            fileRecord.setArticleTitle(station.getTitle()+"《站细》"+str1+"第"+s+"次修改");
            fileRecord.setReviseDate(new Date());
            fileRecord.setPutStation(user.getDeptid());
            fileRecord.setPutPerson(user.getId());
            fileRecord.setExaminePerson(user.getId());
            fileRecord.setArticleEntry(id);
            fileRecordService.save(fileRecord);
            fileManage.setId(id);*/
            //添加修改条目
            QueryWrapper<StandFileList> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("sid",stationId1);
            StandFileList standFileList = standFineListService.getOne(queryWrapper);
            Integer fileListId = standFileList.getId();
            QueryWrapper<FileManage> fileManageQueryWrapper = new QueryWrapper<>();
            fileManageQueryWrapper.eq("stand_fileId",fileListId);
            fileManageQueryWrapper.eq("tree_id",fileManage.getNodeId());
            fileManageQueryWrapper.eq("tree_pid",fileManage.getParentId());
            FileManage fileManage1 = fileManageService.getOne(fileManageQueryWrapper);
            fileManage1.setTreeContext(fileManage.getTreeContext());
            ModifyDetail modifyDetail = new ModifyDetail();
            modifyDetail.setRecordId(num);
            modifyDetail.setModifyPid(fileManage1.getId()-1);
            modifyDetail.setModifyTreeId(fileManage1.getId());
            FileManage fileManage2 = fileManageService.getById(fileManage1.getId() - 1);
            FileManage fileManage3 = fileManageService.getById(fileManage1.getId());
            modifyDetail.setModifyTreeTitle(fileManage2.getTreeTitle());
            modifyDetail.setModifyTreeContext(fileManage3.getTreeContext());
            modifyDetail.setModifyDate(new Date());
            modifyDetail.setModifySid(stationId1);
            modifyDetailService.save(modifyDetail);
            fileManageService.updateById(fileManage1);
            return R.ok().message("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("保存失败");
        }
    }

    //提交数据
    @RequestMapping("updateFileRecord")
    public R updateFileRecord(@RequestParam("stationId") Integer stationId1, FileManage fileManage){
        User user = (User) WebUtils.getSession().getAttribute("user");
        Map<String, Integer> map = loadCoreMaxOrderNum();
        Integer num = map.get("value");
        try {
            if (null!=fileManage.getSid()){
                List<Integer> sid = fileManage.getSid();
                if (sid.size()>0){
                    for (Integer stationId : sid) {
                        QueryWrapper<StandFileList> queryWrapper = new QueryWrapper<>();
                        queryWrapper.eq("sid",stationId);
                        StandFileList standFileList = standFineListService.getOne(queryWrapper);
                        Integer fileListId = standFileList.getId();
                        QueryWrapper<FileManage> fileManageQueryWrapper = new QueryWrapper<>();
                        fileManageQueryWrapper.eq("stand_fileId",fileListId);
                        fileManageQueryWrapper.eq("tree_id",fileManage.getNodeId());
                        fileManageQueryWrapper.eq("tree_pid",fileManage.getParentId());
                        FileManage fileManage1 = fileManageService.getOne(fileManageQueryWrapper);
                        fileManage1.setTreeContext(fileManage.getTreeContext());
                        //添加修改记录
                        QueryWrapper<FileRecord> fileRecordQueryWrapper = new QueryWrapper<>();
                        Date date1 = new Date();
                        SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
                        String str = sdf.format(date1);
                        Date date2 = sdf.parse(str);
                        fileRecordQueryWrapper.eq("revise_date",date2);
                        fileRecordQueryWrapper.eq("article_sid",stationId);
                        List<FileRecord> fileRecords = fileRecordService.list(fileRecordQueryWrapper);
                        if (fileRecords.size()>0) {
                            return R.ok().message("当天修改记录已存在");
                        } else {
                            standFileList.setFrequency(standFileList.getFrequency()+1);
                            standFineListService.updateById(standFileList);
                            FileRecord fileRecord = new FileRecord();
                            Station station = stationService.getById(stationId);
                            Date date = new Date();
                            SimpleDateFormat sdf1 =new SimpleDateFormat("yyyy" );
                            String str1 = sdf1.format(date);
                            String s = ChineseNumToArabicNumUtil.arabicNumToChineseNum(standFileList.getFrequency());
                            fileRecord.setArticleTitle(station.getTitle()+"《站细》"+str1+"第"+s+"次修改");
                            fileRecord.setReviseDate(new Date());
                            fileRecord.setPutPerson(user.getId());
                            fileRecord.setPutStation(user.getDeptid());
                            fileRecord.setExaminePerson(user.getId());
                            fileRecord.setArticleEntry(fileManage1.getId());
                            fileRecord.setArticleSid(stationId);
                            fileRecordService.save(fileRecord);
                        }
                        fileManageService.updateById(fileManage1);
                    }
                }
            }
            //添加修改记录
            FileRecord fileRecord = new FileRecord();
            Station station = stationService.getById(stationId1);
            Date date = new Date();
            SimpleDateFormat sdf1 =new SimpleDateFormat("yyyy");
            String str1 = sdf1.format(date);
            QueryWrapper<StandFileList> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("sid",stationId1);
            StandFileList standFileList = standFineListService.getOne(queryWrapper);
            QueryWrapper<FileRecord> fileRecordQueryWrapper = new QueryWrapper<>();
            Date date1 = new Date();
            SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
            String str = sdf.format(date1);
            Date date2 = sdf.parse(str);
            fileRecordQueryWrapper.eq("revise_date",date2);
            fileRecordQueryWrapper.eq("article_sid",stationId1);
            List<FileRecord> fileRecords = fileRecordService.list(fileRecordQueryWrapper);
            System.out.println("fileRecords.size()==============================="+fileRecords.size()+"revise_date"+date2+"article_sid"+stationId1);
            if(fileRecords.size()>0){
                return R.ok().message("当天修改记录已存在");
            }else{
                standFileList.setFrequency(standFileList.getFrequency()+1);
                standFineListService.updateById(standFileList);
                String s = ChineseNumToArabicNumUtil.arabicNumToChineseNum(standFileList.getFrequency());
                fileRecord.setArticleTitle(station.getTitle()+"《站细》"+str1+"第"+s+"次修改");
                fileRecord.setReviseDate(new Date());
                fileRecord.setPutStation(user.getDeptid());
                fileRecord.setPutPerson(user.getId());
                fileRecord.setExaminePerson(user.getId());
                fileRecord.setArticleSid(stationId1);
                /*fileRecord.setArticleEntry(id);*/
                fileRecordService.save(fileRecord);
            }
            /*fileManage.setId(id);*/
            //添加修改条目
            Integer fileListId = standFileList.getId();
            QueryWrapper<FileManage> fileManageQueryWrapper = new QueryWrapper<>();
            fileManageQueryWrapper.eq("stand_fileId",fileListId);
            fileManageQueryWrapper.eq("tree_id",fileManage.getNodeId());
            fileManageQueryWrapper.eq("tree_pid",fileManage.getParentId());
            FileManage fileManage1 = fileManageService.getOne(fileManageQueryWrapper);
            fileManage1.setTreeContext(fileManage.getTreeContext());
            fileManageService.updateById(fileManage1);
            return R.ok().message("提交成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("提交失败");
        }
    }
    //加载最大的排序码
    @RequestMapping("loadCoreMaxOrderNum")
    public Map<String ,Integer> loadCoreMaxOrderNum(){
        Map<String,Integer> map = new HashMap<>();
        QueryWrapper<ModifyDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("record_id");
        List<ModifyDetail> list = modifyDetailService.list(queryWrapper);

        if(list.size()>0){
            map.put("value",list.get(0).getRecordId()+1);
        }else{
            map.put("value",1);
        }
        return map;
    }
    //根据Id查询编制规则
    @RequestMapping("findFileManageById/{id}")
    public R findStandFileById(@PathVariable("id") Integer id){
        FileManage fileManage = fileManageService.getById(id);
        return  R.ok().data("item",fileManage);
    }
}

